package com.jn.algorithm.day001.question3;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author 江南大学1033190417
 * @date 2022/4/15 16:31
 */
public class Solution {
    public int lengthOfLongestSubstring(String s) {
        String result = "";
        String bestString = "";
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (result.indexOf(c) == -1) {//如果当前字符不在result中
                result += c;
            } else {
                //移动指针
                i = s.indexOf(c);
                if (result.length() > bestString.length()) {
                    bestString = result;
                }
                result = "";
            }
        }
        if (result.length() > bestString.length())
            bestString = result;
        System.out.println(bestString);
        return bestString.length();
    }

    public int correctAnswer(String s) {
        Set<Character> occ = new HashSet<>();
        int length = s.length();
        int rk = -1,//右指针，字符串右边界左侧，没开始移动
                ans = 0;//最大值
        for (int i = 0; i < length; i++) {
            if (i != 0)
                occ.remove(s.charAt(i - 1));

            while (rk + 1 < length && !occ.contains(s.charAt(rk + 1))) {
                occ.add(s.charAt(rk + 1));
                rk++;
            }
            ans = Math.max(ans, rk - i + 1);
        }
        return ans;
    }

    public int myAnswer1(String s) {
        Set<Character> set = new HashSet<>();
        int rk = -1, ans = 0;
        int length = s.length();
        for (int i = 0; i < length; i++) {
            if (i != 0)
                set.remove(s.charAt(i - 1));
            while (rk + 1 < length && !set.contains(s.charAt(rk + 1))) {
                set.add(s.charAt(rk + 1));
                rk++;
            }
            ans = Math.max(ans, rk - i + 1);
        }
        return ans;
    }

    public int myAnswer2(String s) {
        int ans = 0;
        Map<Character, Integer> map = new HashMap<>();
        int length = s.length();
        for (int i = 0; i < length; i++) {
            map.put(s.charAt(i), i);
            for (int j = i + 1; j < length; j++) {
                if (j == length - 1) {
                    ans = Math.max(ans, map.size());
                }
                if (!map.containsKey(s.charAt(j))) {
                    map.put(s.charAt(j), j);
                } else {
                    ans = Math.max(ans, map.size());
                    map.clear();
                    j = length;//旨在退出内层循环
                }
            }
        }
        ans = Math.max(ans, map.size());//正对如果字符串长度只为1，就不会走第二个循环
        return ans;
    }

    public int myAnswer3(String s) {
        int ans = 0;
        String res = "";
        int length = s.length();
        for (int i = 0; i < length; i++) {
            res += s.charAt(i);
            for (int j = i + 1; j < length; j++) {
                if (j == length - 1) {
                    if (res.indexOf(s.charAt(j)) == -1) {
                        res += s.charAt(j);
                    }
                    ans = Math.max(ans, res.length());
                }
                if (res.indexOf(s.charAt(j)) == -1) {
                    res += s.charAt(j);
                } else {
                    ans = Math.max(ans, res.length());
                    res = "";
                    j = length;//旨在退出内层循环
                }
            }
        }
        ans = Math.max(ans, res.length());//正对如果字符串长度只为1，就不会走第二个循环
        return ans;
    }

    public static void main(String[] args) {
        String s = "pwwkew";
        Solution solution = new Solution();
        System.out.println(solution.myAnswer3(s));
    }
}
